﻿// CSplashScreenState.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
#endregion

namespace Casters.source.States
{
    public class CSplashScreenState : CBaseState
    {
        protected bool m_bFadeIn;
        protected float m_fScreenTimer = 0f;
        protected float m_fMaxScreenTime = 4f;
        protected Vector2 m_CameraOffset;

        protected Texture2D m_StudioLogo = null;

        private static CSplashScreenState instance = null;
        public static CSplashScreenState GetInstance()
        {
            if (instance == null)
                instance = new CSplashScreenState();

            return instance;
        }

        CSplashScreenState() { }

        /// <summary>
        /// Setup the state
        /// </summary>
        public override void Initialize()
        {
            m_bFadeIn = true;

            m_CameraOffset = CGame.GetInstance().m_Camera.m_vPosition;

            base.Initialize();
        }

        /// <summary>
        /// Load all resources needed for this state
        /// </summary>
        public override void LoadContent()
        {
            m_StudioLogo = CGame.GetInstance().Content.Load<Texture2D>("SplashScreens/studiologo");

            CEventSystem.RegisterEventHandler(EventID.Confirm, OnConfirm);

            base.LoadContent();
        }

        /// <summary>
        /// Unload anything needed before exiting this state
        /// </summary>
        public override void UnloadContent()
        {
            CEventSystem.UnregisterEventHandler(EventID.Confirm, OnConfirm);

            base.UnloadContent();
        }

        /// <summary>
        /// Run update logic for this state
        /// </summary>
        /// <param name="dt">Delta Time</param>
        public override void Update(float dt)
        {
            if (m_bFadeIn)
                m_fScreenTimer += dt;
            else
                m_fScreenTimer -= dt;

            if (m_fScreenTimer >= m_fMaxScreenTime)
            {
                m_fScreenTimer = m_fMaxScreenTime;
                m_bFadeIn = false;
            }
            else if (m_fScreenTimer <= 0f)
            {
                m_fScreenTimer = 0;
                m_bFadeIn = true;
            }

            base.Update(dt);
        }

        /// <summary>
        /// Draw stuff to the screen
        /// </summary>
        public override void Draw()
        {
            CRenderEngine.Draw(m_StudioLogo, Vector2.Zero + m_CameraOffset, Color.White * (m_fScreenTimer / m_fMaxScreenTime));
            CRenderEngine.DrawString(CGame.GetInstance().Content.Load<SpriteFont>("Fonts/Arial"), "ScreenTimer: " + m_fScreenTimer.ToString(), new Vector2(10f, 10f) + m_CameraOffset, Color.Green);
            base.Draw();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventArgs"></param>
        public void OnConfirm(params object[] eventArgs)
        {
            CEventSystem.BroadcastEvent(EventID.ChangeState, CLoginState.GetInstance());
        }
    }
}
